﻿using Ionic.Zip;
using System;
using System.IO;
using System.Linq;
using System.Text;

public static class ZipHelper
{
    /// <summary> 
    /// 压缩指定文件或目录 
    /// </summary> 
    /// <param name="fileOrDirectoryName">要进行压缩的文件或目录名称</param> 
    /// <returns>生成的压缩文件名</returns> 
    public static string Compress(string fileOrDirectoryName)
    {
        string zipPath = GetZipPath(fileOrDirectoryName);
        Compress(fileOrDirectoryName, zipPath);
        return zipPath;
    }

    /// <summary> 
    /// 压缩指定文件或目录 
    /// </summary> 
    /// <param name="fileOrDirectoryName">要进行压缩的文件或目录名称</param> 
    /// <param name="zipPath">生成的压缩文件路径</param> 
    public static void Compress(string fileOrDirectoryName, string zipPath)
    {
        using (ZipFile zip = new ZipFile(Encoding.GetEncoding("utf-8")))
        {
            zip.AddItem(fileOrDirectoryName, "");
            zip.Save(zipPath);
        }
    }

    /// <summary> 
    /// 分卷压缩指定文件或目录 
    /// </summary> 
    /// <param name="fileOrDirectoryName">要进行压缩的文件或目录名称</param> 
    /// <param name="segmentSize">分卷大小(MB)</param> 
    public static void Compress(string fileOrDirectoryName, int segmentSize)
    {
        string zipPath = GetZipPath(fileOrDirectoryName);
        Compress(fileOrDirectoryName, zipPath, ZipDataUnit.MB, segmentSize);
    }

    /// <summary> 
    /// 分卷压缩指定文件或目录，可指定最后zip路径 
    /// </summary> 
    /// <param name="fileOrDirectoryName">要进行压缩的文件或目录名称</param> 
    /// <param name="segmentSize">分卷大小(MB)</param> 
    public static void Compress(string fileOrDirectoryName, string customZipPath, int segmentSize)
    {
        string zipPath = customZipPath;
        Compress(fileOrDirectoryName, zipPath, ZipDataUnit.MB, segmentSize);
    }

    /// <summary> 
    /// 分卷压缩指定文件或目录 
    /// </summary> 
    /// <param name="fileOrDirectoryName">要进行压缩的文件或目录名称</param> 
    /// <param name="zipPath">生成的压缩文件路径</param> 
    /// <param name="dataUnit">分卷数据单位</param> 
    /// <param name="segmentSize">分卷大小</param> 
    public static void Compress(string fileOrDirectoryName, string zipPath, ZipDataUnit dataUnit, int segmentSize)
    {
        try
        {
            using (ZipFile zip = new ZipFile(Encoding.GetEncoding("utf-8")))
            {
                zip.MaxOutputSegmentSize = (int)dataUnit * segmentSize;
                zip.UseZip64WhenSaving = Zip64Option.Always;
                zip.BufferSize = 1024;
                zip.CaseSensitiveRetrieval = true;
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                zip.AddItem(fileOrDirectoryName, "");
                zip.Save(zipPath);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static string GetZipPath(string path)
    {
        string directory = Path.GetDirectoryName(path);
        if (string.IsNullOrWhiteSpace(directory)) directory = Path.GetPathRoot(path);
        string fileName = Path.GetFileName(path);
        string zipFileName = null;
        //文件路径 
        if (!string.IsNullOrWhiteSpace(fileName))
        {
            zipFileName = Path.ChangeExtension(fileName, ".zip");
        }
        else
        {
            zipFileName = directory.Split('\\').Last();
            if (string.IsNullOrEmpty(zipFileName))
            {
                zipFileName = "未命名";
            }
            else
            {
                directory = directory.Replace(zipFileName, "");
            }
            zipFileName = ".zip";
        }

        return Path.Combine(directory, zipFileName);
    }

    /// <summary>
    /// 解压文件
    /// </summary>
    /// <param name="strZipPath">zip文件路径</param>
    /// <param name="strUnZipPath">解压文件夹</param>
    public static void DeCompress(string strZipPath, string strUnZipPath)
    {

        bool overWrite = true;//设置是否覆盖文件

        using (ZipFile zip = ZipFile.Read(strZipPath))
        {
            foreach (ZipEntry entry in zip)
            {
                if (string.IsNullOrEmpty(strUnZipPath))
                {
                    strUnZipPath = strZipPath.Split('.')[0];
                }
                if (overWrite)
                {
                    entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件，如果已存在就覆盖
                }
                else
                {
                    entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件，如果已存在不覆盖
                }
            }
        }
    }
}
/// <summary> 
/// 分卷数据单位 
/// </summary> 
public enum ZipDataUnit
{
    /// <summary> 
    /// 1024字节 
    /// </summary> 
    KB = 1024,

    /// <summary> 
    /// 1024 * 1024字节 
    /// </summary> 
    MB = 1024 * 1024,

    /// <summary> 
    /// 1024 * 1024 * 1024字节 
    /// </summary> 
    GB = 1024 * 1024 * 1024
}